package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsDetailMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class NewsDetailServiceImpl implements INewsDetailService {

    @Autowired
    private NewsDetailMapper newsDetailMapper;

    @Autowired
    private NewsContentMapper newsContentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<NewsDetail> list = newsDetailMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public NewsContent getContentById(Long id) {
        return newsContentMapper.selectForId(id);
    }

    @Override
    public void saveorUpdate(NewsDetail newsDetail) {
        //查看序号是否有值
        if (newsDetail.getSequence() == null) {
            //查看日报中按照类型的分类的序号最大是多少
            Long sequence = newsDetailMapper.selectSequenceBynewsCatalogId(newsDetail.getNewsCatalog().getId());
            //如果没有序号,设置为0
            if (sequence == null) {
                sequence = 0L;
            }
            newsDetail.setSequence(sequence + 1);
        }
        Date data = new Date();
        //如果是发布状态和推荐状态,则设置发布时间
        if (NewsDetail.STATE_RELEASE.equals(newsDetail.getState()) || NewsDetail.STATE_RECOMMEND.equals(newsDetail.getState())) {
            newsDetail.setReleaseTime(data);
        }

        if (newsDetail.getId() == null) {
            newsDetail.setCreateTime(data);
            newsDetailMapper.insert(newsDetail);
            //设置主键关联,保存日报内容
            NewsContent newsContent = newsDetail.getNewsContent();
            newsContent.setId(newsDetail.getId());
            newsContentMapper.insert(newsContent);
        } else {

            newsDetailMapper.updateByPrimaryKey(newsDetail);
            //设置主键关联,保存日报内容
            NewsContent newsContent = newsDetail.getNewsContent();
            newsContent.setId(newsDetail.getId());
            newsContentMapper.updateByPrimaryKey(newsContent);
        }
    }

    @Override
    public List<NewsDetail> selectNewsByState(Integer state) {
        return newsDetailMapper.selectNewsByState(state);

    }

    @Override
    public NewsDetail get(Long id) {
        //指定id查询日报
        NewsDetail newsDetail = newsDetailMapper.selectByPrimaryKey(id);
        //指定id查询日报内容
        NewsContent newsContent = newsContentMapper.selectForId(id);
        newsDetail.setNewsContent(newsContent);
        return newsDetail;
    }

    //指定id和type查询
    @Override
    public int[] selectCollectId(Long id, int type) {

        return newsDetailMapper.selectId(id, type);
    }



    @Override
    public PageInfo selectCollect(CollectQueryObject qo) {

        List list = new ArrayList();
        //指定id和type查询collect中日报id数组
        int[] newDetailsId = newsDetailMapper.selectId(qo.getUserId(), qo.getType());
        //判断 如果结果不为空 和 类型为日报类型
        if (newDetailsId.length != 0 && qo.getType() == 1) {
            //指定id数组查询日报内容 (分页)
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
            list = newsDetailMapper.selectDetailByids(newDetailsId);
        }
        //指定id和type查询collect中攻略id数组
        int[] strategyId = newsDetailMapper.selectId(qo.getUserId(), qo.getType());
        //判断 如果结果不为空 和 类型为攻略类型
        if (strategyId.length != 0 && qo.getType() == 2) {
            //指定id数组查询攻略内容
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
            list = strategyMapper.selectStrategyByids(strategyId);
        }
        //指定id和type查询collect中游记id数组
        int[] travelId = newsDetailMapper.selectId(qo.getUserId(), qo.getType());
        //判断 如果结果不为空 和 类型为游记类型
        if (travelId.length != 0 && qo.getType() == 3) {
            //指定id数组查询游记内容
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
            list = travelMapper.selectTravelByids(travelId);
        }
        return new PageInfo(list);

    }
}
